package com.example.cookie02;

import sun.plugin.dom.core.Element;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

import static java.nio.file.Files.getAttribute;

@WebServlet("/test")
public class HelloServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String cookie1 = req.getParameter("cookie");



        if(cookie1!=null&&cookie1.equals("yes")){
            //设置cookie
            Cookie cookie = new Cookie("username", username+"-"+password);
            cookie.setMaxAge(30);
            cookie.setDomain("localhost");
            resp.addCookie(cookie);



            req.getRequestDispatcher("/show").forward(req, resp);
        } else if (username!=null&&password!=null&&username.equals("罗")&&password.equals("123456")) {
            req.getRequestDispatcher("/show").forward(req, resp);
        }else {
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }
    }
    public void destroy() {
    }
}